home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1451.dms / var1451.adf / PrinterDevice / Example2.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  12KB  |  353 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Printer Device              Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program demonstrates how you can use the Printer   */
  23. /* Device to send (raw as well as translated) text to a    */
  24. /* printer. However, instead of waiting for our request to */
  25. /* be completed as in Example 1, we use asynchronous       */
  26. /* requests.                                               */
  27.  
  28.  
  29.  
  30. #include <exec/types.h>       /* Data types.             */
  31. #include <exec/errors.h>      /* Exec error messages.    */
  32. #include <devices/printer.h>  /* Printer Device.         */
  33. #include <exec/io.h>          /* Standard request block. */
  34.  
  35.  
  36.  
  37. #define LINE_LENGTH    44 /* 44 characters/line. */
  38. #define BUFFER_SIZE 44*40 /* 40 lines.           */
  39.  
  40.  
  41.  
  42. /* Declare how the printer request block look like: */
  43. union printerIO
  44. {
  45.   struct IOStdReq ios;
  46.   struct IODRPReq iodrp;
  47.   struct IOPrtCmdReq iopc;
  48. };
  49.  
  50. /* Declare a pointer to our reply port: */
  51. struct MsgPort *replymp = NULL;
  52.  
  53. /* Declare a pointer our printer request block: */
  54. union printerIO *printer_req = NULL;
  55.  
  56. /* Store the printer device error here: */
  57. UWORD printer_dever = TRUE;
  58.  
  59. /* Declare our data buffer: */
  60. BYTE buffer[BUFFER_SIZE];
  61.  
  62.  
  63.  
  64. /* Declare our functions: */
  65.  
  66. /* Our main function: */
  67. void main();
  68.  
  69. /* Clears and removes everything nice and neatly: */
  70. void clean_up( BYTE error, STRPTR text );
  71.  
  72. /* Prints some information about the error: */
  73. void PrtError( BYTE error );
  74.  
  75. /* Sends characters (which are translated) to the printer: */
  76. void PrintTextNoWait(
  77.   union printerIO *ioreq,
  78.   BYTE *data,
  79.   ULONG length
  80. );
  81.  
  82. /* Sends raw (untranslated) characters to the printer: */
  83. void PrintRawNoWait(
  84.   union printerIO *ioreq,
  85.   BYTE *data,
  86.   ULONG length
  87. );
  88.  
  89.  
  90.  
  91. void main()
  92. {
  93.   int x, y;
  94.   union printerIO *ptr;
  95.  
  96.  
  97.   
  98.   /* Get a reply port: (No name, priority 0) */
  99.   replymp = (struct MsgPort *)
  100.     CreatePort( NULL, 0 );
  101.   if( !replymp )
  102.     clean_up( 0, "Could not create the reply port!" );
  103.  
  104.  
  105.  
  106.   /* Create the printer request block: */
  107.   printer_req = (union printerIO *)
  108.     CreateExtIO( replymp, sizeof(union printerIO) );
  109.   if( !printer_req )
  110.     clean_up( 0, "Not enough memory for the printer request block!" );
  111.  
  112.  
  113.  
  114.   /* Open the Printer Device: */
  115.   printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
  116.   if( printer_dever )
  117.     clean_up( 0, "Could not open the Printer Device!" );
  118.  
  119.  
  120.  
  121.   /* Fill the buffer with characters. 40 lines with the text: */
  122.   /* "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ".           */
  123.   for( y = 0; y < BUFFER_SIZE; y += LINE_LENGTH )
  124.   {
  125.     /* Print 43 characters: (ASCII: 0-Z) */
  126.     for( x = 48; x < 91; x++ )
  127.       buffer[ y + x-48 ] = x;
  128.  
  129.     /* Put a line feed (ASCII 10: LF) after each line: */ 
  130.     buffer[ y + 43 ] = 10;
  131.   }
  132.  
  133.  
  134.  
  135.   /* Send some text to the printer: (Will be translated) */
  136.   PrintTextNoWait( printer_req, buffer, BUFFER_SIZE ); 
  137.  
  138.  
  139.   /* As long as the pointer is not pointing to */
  140.   /* the request we should stay in the loop:   */
  141.   ptr = NULL;
  142.   while( ptr == NULL )
  143.   {
  144.     /*   ... do something ...    */
  145.     /* Well, I do not know what. */
  146.   
  147.     /* Check if the request has been completed: (If the  */
  148.     /* request has been compleded CheckIO() will return  */
  149.     /* a pointer to the request, else NULL is returned.) */
  150.     ptr = (union printerIO *) CheckIO( printer_req );
  151.   }
  152.  
  153.   /* At last the request was completed! */
  154.  
  155.  
  156.   /* Remove the requstblock's message. (The ptr and     */
  157.   /* printer_req are in this example identical, so it   */
  158.   /* does not matter whichever you use. The paranteces  */
  159.   /* around the expression is actually unnecessary, but */
  160.   /* this looks better.)                                */
  161.   Remove( &(ptr->ios.io_Message.mn_Node) );
  162.  
  163.  
  164.   /* Check if everything is OK? */
  165.   if( ptr->ios.io_Error )
  166.     PrtError( ptr->ios.io_Error );
  167.   else
  168.     printf( "Data sent without any problems!\n" );
  169.  
  170.  
  171.   /* Clean up and quit: */
  172.   clean_up( 0, "The End!" );
  173. }
  174.  
  175.  
  176.  
  177. /* Close and return everything that has been */
  178. /* opened and allocated before we quit:      */
  179.  
  180. void clean_up( BYTE error, STRPTR text )
  181. {
  182.   /* Print some information about the problem: */
  183.   if( error )
  184.     PrtError( error );
  185.  
  186.   /* Close the Printer Device: */ 
  187.   if( !printer_dever )
  188.     CloseDevice( printer_req );
  189.  
  190.   /* Deallocate the printer request block: */
  191.   if( printer_req )
  192.     DeleteExtIO( printer_req, sizeof(union printerIO) );
  193.  
  194.   /* Remove the replyport: */
  195.   if( replymp )
  196.     DeletePort( replymp);
  197.  
  198.   /* Print the message: */
  199.   printf( "\n%s\n", text );
  200.  
  201.   /* Quit: */
  202.   exit( 0 );
  203. }
  204.  
  205.  
  206.  
  207. /* PrtError() tells the user what went wrong. You give it the error code */
  208. /* you received, and PrtError() will print a short description of the    */
  209. /* problem. Useful when debugging. (Printer errors)                      */
  210. /*                                                                       */
  211. /* Synopsis: PrtError( error );                                          */
  212. /*                                                                       */
  213. /* error:    (BYTE) The error value you want to have explained.          */
  214.  
  215. void PrtError( BYTE error )
  216. {
  217.   switch( error )
  218.   {
  219.     /* EXEC error messages: (defined in "exec/errors.h") */
  220.     case IOERR_OPENFAIL:
  221.       printf( "Could not open the device!\n" );
  222.       break;
  223.  
  224.     case IOERR_ABORTED:
  225.       printf( "The request was aborted!\n" );
  226.       break;
  227.  
  228.     case IOERR_NOCMD:
  229.       printf( "Unknown Command!\n" );
  230.       break;
  231.  
  232.     case IOERR_BADLENGTH:
  233.       printf( "Bad length of the command - data!\n" );
  234.  
  235.  
  236.     /* Printer Device errors: (defined in "devices/printer.h") */
  237.     case PDERR_CANCEL:
  238.       printf( "User cancelled the request!\n" );
  239.       break;
  240.       
  241.     case PDERR_NOTGRAPHICS:
  242.       printf( "The printer does not support graphics!\n" );
  243.       break;
  244.  
  245.     case PDERR_BADDIMENSION:
  246.       printf( "The printer dimension is not valid!\n" );
  247.       break;
  248.       
  249.  
  250.     case PDERR_INTERNALMEMORY:
  251.       printf( "Not enough memory for the internal printer functions!\n" );
  252.       break;
  253.  
  254.     case PDERR_BUFFERMEMORY:
  255.       printf( "Not enough memory for the print buffer!\n" );
  256.       break;
  257.  
  258.     default:
  259.       printf( "An unknown error was reported! Error nr: %d\n", error );
  260.   }
  261. }
  262.  
  263.  
  264.  
  265. /* PrintTextNoWait() sends characters (which will be translated)  */
  266. /* to the printer, but will not wait for the request to be        */
  267. /* completed. Remember that all requests that you have started    */
  268. /* must have been completed or aborted before your program may    */
  269. /* terminate. (Do not forget to remove the message.)              */
  270. /*                                                                */
  271. /* Since the printer device will use the Preference's settings,   */
  272. /* it will know to which port (parallel or serial) the printer is */
  273. /* connected to, what type of printer it is, and what special     */
  274. /* settings (margins, density, quality mode etc) the user have    */
  275. /* defined.                                                       */
  276. /*                                                                */
  277. /* Note! All characters which are sent with this function may be  */
  278. /* translated by Preferences before it is passed on to the        */
  279. /* printer.                                                       */
  280. /*                                                                */
  281. /* Synopsis: PrintTextNoWait( io, data, length );                 */
  282. /*                                                                */
  283. /* io:       (struct IOStdReq *) Pointer to the printer request   */
  284. /*           block.                                               */
  285. /*                                                                */
  286. /* data:     (BYTE *) Pointer to the first character that should  */
  287. /*           be printed.                                          */
  288. /*                                                                */
  289. /* length    (ULONG) How many characters (bytes) you want to send */
  290. /*           to the printer.                                      */
  291.  
  292. void PrintTextNoWait(
  293.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  294.   BYTE *data,             /* Pointer to the data which should be printed.   */
  295.   ULONG length            /* How many characters (bytes) should be printed. */
  296. )
  297. {
  298.   /* We want to print some text: (send data to PRT:) */
  299.   ioreq->ios.io_Command = CMD_WRITE;
  300.  
  301.   /* Give the start address of our data: */
  302.   ioreq->ios.io_Data = (APTR) data;
  303.  
  304.   /* Set number of chracters that should be printed: */
  305.   ioreq->ios.io_Length = length;
  306.  
  307.   /* Do our request and return immediately: */
  308.   SendIO( ioreq );
  309. }
  310.  
  311.  
  312.  
  313. /* PrintRawNoWait() sends raw (untranslated) characters to the  */
  314. /* printer, but will not wait for the request to be completed.  */
  315. /* Remember that all requests that you have started must have   */
  316. /* been completed or aborted before your program may terminate. */
  317. /* (Do not forget to remove the message.)                       */
  318. /*                                                              */
  319. /* Note that sending untranslated characters is usually not a   */
  320. /* very good idea. Since the characters will not be translated  */
  321. /* by Preferences, you can not be sure that the characters you  */
  322. /* send will be the same when printed.                          */ 
  323. /*                                                              */
  324. /* Synopsis: PrintRawNoWait( io, data, length );                */
  325. /*                                                              */
  326. /* io:       (struct IOStdReq *) Pointer to the printer request */
  327. /*           block.                                             */
  328. /*                                                              */
  329. /* data:     (BYTE *) Pointer to the first character that       */
  330. /*           should be printed.                                 */
  331. /*                                                              */
  332. /* length    (ULONG) How many characters (bytes) you want to    */
  333. /*           send to the printer.                               */
  334.  
  335. void PrintRawNoWait(
  336.   union printerIO *ioreq, /* Pointer to the printer request block.          */
  337.   BYTE *data,             /* Pointer to the data which should be printed.   */
  338.   ULONG length            /* How many characters (bytes) should be printed. */
  339. )
  340. {
  341.   /* We want to print some raw (untranslated) text: */
  342.   ioreq->ios.io_Command = PRD_RAWWRITE;
  343.  
  344.   /* Give the start address of our data: */
  345.   ioreq->ios.io_Data = (APTR) data;
  346.  
  347.   /* Set number of chracters that should be printed: */
  348.   ioreq->ios.io_Length = length;
  349.  
  350.   /* Do our request and return immediately: */
  351.   SendIO( ioreq );
  352. }
  353.